function quickSort(nums){

    function loop(arr){
       let len = arr.length
       if(len < 2) return arr
       let baseIndex = Math.floor(len/2)
       let baseVal = arr[baseIndex]
       let minArr = []
       let maxArr = []
       for (let i = 0; i < len; i++) {
            if(arr[i] <= baseVal && i !== baseIndex){
                minArr.push(arr[i])   
            }

            if(arr[i] >= baseVal && i !== baseIndex){
                maxArr.push(arr[i])   
            }
       }
       return [...loop(minArr), baseVal, ...loop(maxArr)]
    }

    return loop(nums)

}


const nums = [2,1,6,7,8,3,5,4]
console.log(quickSort(nums))